var express = require('express');
var router = express.Router();
var Article = require('../model/article_list');
var clearHtml = require('../utils/clearHtml');
require('../utils/dateFormat');

// 增加文章
router.post('/add', async (req, res) => {
  let { classifyIds, userId } = req.body;
  try {
    if (!classifyIds || !userId) {
      res.json({
        resultCode: '0002',
        resultMessage: '参数错误',
        result: ''
      })
    } else {
      const c1 = Math.floor(Math.random() * 1000);
      const c2 = Math.floor(Math.random() * 1000);
      const news = await Article.insertMany({
        userId: userId,
        classifyId: classifyIds,
        articleId: `${c1}${(Date.parse(new Date())) / 1000}${c2}`,
        articleTitle: '',
        articleContent: '',
        createTime: new Date().Format('yyyy-MM-dd hh:mm:ss')
      })
      res.json({
        resultCode: '0000',
        resultMessage: '添加成功',
        result: news
      })
    }
  } catch (err) {
    res.json({
      resultCode: '9999',
      resultMessage: err.message,
      result: ''
    })
  }
})

// 修改文章
router.post('/edit', async (req, res) => {
  var { userId, classifyId, articleId, articleTitle, articleContent } = req.body;
  try {
    if (!userId || !classifyId || !articleId || !articleTitle || !articleContent) {
      res.json({
        resultCode: '0002',
        resultMessage: '参数错误',
        result: ''
      })
    } else {
      await Article.updateOne({
        userId: userId,
        classifyId: classifyId,
        articleId: articleId
      }, {
        articleTitle: articleTitle,
        articleContent: articleContent,
        status: '0'
      })
      res.json({
        resultCode: '0000',
        resultMessage: '保存成功',
        result: ''
      })
    }
  } catch(err) {
    res.json({
      resultCode: '9999',
      resultMessage: err.message,
      result: ''
    })
  }
})

// 请求文章列表
router.get('/search', async (req, res) => {
  var { userId, classifyId } = req.query;
  try {
    if (!userId || !classifyId) {
      res.json({
        resultCode: '0002',
        resultMessage: '参数错误',
        result: ''
      })
    } else {
      var list = await Article.find({
        userId: userId,
        classifyId: classifyId
      })
      var newList = Array(list.length).fill({})
      list.forEach(function(item, index) {
        newList[index] = {}
        newList[index].articleContent = item.articleContent
        newList[index].articleTitle = item.articleTitle
        newList[index].articleId = item.articleId
        newList[index].classifyId = item.classifyId
        newList[index].createTime = item.createTime
        newList[index].status = item.status
        newList[index].userId = item.userId
        newList[index].world = clearHtml(item.articleContent)
        // console.log(item.articleContent)
      })
      // console.log(newList)
      res.json({
        resultCode: '0000',
        resultMessage: '查询成功',
        result: newList
      })
    }
  } catch(err) {
    res.json({
      resultCode: '9999',
      resultMessage: err.message,
      result: ''
    })
  }
})

// 删除文章
router.post('/delete', async (req, res) => {
  const { userId, classifyId, articleId } = req.body
  try {
    if (!userId || !classifyId || !articleId) {
      res.json({
        resultCode: '0002',
        resultMessage: '参数错误',
        result: ''
      })
    } else {
      await Article.deleteOne({
        userId,
        classifyId,
        articleId
      })
      res.json({
        resultCode: '0000',
        resultMessage: '删除成功',
        result: ''
      })
    }
  } catch(err) {
    res.json({
      resultCode: '9999',
      resultMessage: err.message,
      result: ''
    })
  }
})

// 移动文章分类
router.post('/move', async (req, res) => {
  var { userId, classifyId, articleId, newClassifyId } = req.body
  try {
    if (!userId || !classifyId || !articleId) {
      res.json({
        resultCode: '0002',
        resultMessage: '参数错误',
        result: ''
      })
    } else {
      await Article.updateOne({
        userId,
        classifyId,
        articleId
      }, {
        classifyId: newClassifyId
      })
      res.json({
        resultCode: '0000',
        resultMessage: '修改成功',
        result: ''
      })
    }
  } catch(err) {
    res.json({
      resultCode: '9999',
      resultMessage: err.message,
      result: ''
    })
  }
})
module.exports = router;